package com.cmwa.scbp.base.mvc.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;

public abstract class BaseController {
	
	protected Map<String, String> getParams(HttpServletRequest request) {
		Map<String, String> params = new HashMap<String, String>();
		try{
			Enumeration<String> keys = request.getParameterNames();
			if(keys==null){
				return params;
			}
			while(keys.hasMoreElements()){
				String key = keys.nextElement();
				String value = new String(((String)request.getParameter(key)).getBytes("ISO-8859-1"), "UTF-8");
				if(StringUtils.isNotEmpty(value)){
					params.put(key, value);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return params;
	}
	
	protected void paramModel(HttpServletRequest request,ModelMap model) {
		try{
			Enumeration<String> keys = request.getParameterNames();
			if(keys==null){
				return;
			}
			while(keys.hasMoreElements()){
				String key = keys.nextElement();
				String value = new String(((String)request.getParameter(key)).getBytes("ISO-8859-1"), "UTF-8");
				if(StringUtils.isNotEmpty(value)){
					model.addAttribute(key, value);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 重定向到成功页面
	 * @return
	 */
	protected String redirectSuccess() {
		return "system/hint/success";
	}
	
	/**
	 * 执行失败，跳转到失败页面
	 * @return
	 */
	protected String redirectExecFaild() {
		return "system/hint/validateFailed";
	}
	
	protected String redirectCustom() {
		return "custom";
	}
}
